<?php

/**
 * Description of
 *
 * @author yuanjun.liu
 */
class Permission {

    static public function table() {
        return Db::I()->table('permission_relationship');
    }

    static public function update($type, $id, $permissions) {
        if (false === self::delete($type, $id))
            return false;
        $permissions = array_filter(array_unique($permissions));
        $datas = array();
        foreach ($permissions as $val) {
            $datas[] = array('type' => $type, 'id' => $id, 'permission' => $val);
        }
        return self::table()->insertAll($datas);
    }

    static public function delete($type, $id) {
        return self::table()->where(array('_logic' => 'AND', array('type', 'eq', $type), array('id', 'eq', $id)))->delete();
    }

    static public function get($type, $id) {
        is_array($id) || $id = array($id);
        $query = self::table()->where(array('_logic' => 'AND', array('type', 'eq', $type), array('id', 'in', $id)))->select();
        if ($query) {
            $permissions = array();
            if (!empty($query)) {
                foreach ($query as $val) {
                    $permissions[] = $val['permission'];
                }
            }
            return $permissions;
        }
        return false;
    }
    
    static public function check($user) {
        
    }

}

?>
